﻿using System;
using System.Windows;
using Microsoft.SqlServer.Server;
using YwangHRMSys.DAL;
using YwangHRMSys.Model;


namespace YwangHRMSys.UI.SystemMgr.ContractMgr
    {
        /// <summary>
        ///     ContractInfoWindowEdit.xaml 的交互逻辑
        /// </summary>
        public partial class ContractInfoWindowEdit : Window
            {

                public ContractInfoWindowEdit()
                {
                    InitializeComponent();
                }

                public bool IsInsert { set; get; }
                public Guid EditGuid { set; get; }

                DateTime signTime;
            /// <summary>
            /// 列出还未签订合同员工的信息
            /// </summary>
                private void Loaddata()
                {
                    CmbEmployeeName.ItemsSource = new EmployeeInfoDAL().GetEmployeeByGuid();
                    
                }

                private void Window_Loaded( object sender, RoutedEventArgs e )
                {
                    //  CmbEmployeeName.ItemsSource = new EmployeeInfoDAL().ListAll();
          
                    if (IsInsert)
                        {
                                 Loaddata();
                        }
                    else
                        {
                            var dal = new ContractInfoDAL();
                            ContractInfoModel model = dal.GetByContractInfoID(EditGuid);
                            EmployeeInfoDAL employeeDal=new EmployeeInfoDAL();
                            EmployeeInfoModel[] employeeModels = employeeDal.GetByEmployeeInfoId(model.EmployeeNameId);
                            CmbEmployeeName.ItemsSource = employeeModels;
                            SignedDateTime.SelectedDate = model.ContractSigned;
                            ExpiresDateTime.SelectedDate = model.ContractExpires;
                            LabelYear.Text = model.ContractLength;
                            txtRemarks.Text = model.ContractRemarks;

                        }


                }


                private void BtnSave_Click( object sender, RoutedEventArgs e )
                {
                    var de = new ContractInfoModel();
                    string dateFormat = string.Format("yyyy年MM月dd日");
                    #region 添加功能
                    if (IsInsert)
                        {

                            if (SignedDateTime.SelectedDate != null)
                                {
                                    
                                    de.ContractSigned = (DateTime)SignedDateTime.SelectedDate;
                                    if (ExpiresDateTime.SelectedDate != null)
                                        {
                                            de.ContractExpires = (DateTime)ExpiresDateTime.SelectedDate;
                                        }
                                }
                            else
                                {
                                    MessageBox.Show("合同时间不能为空");
                                return;
                                    
                                }
                            Guid id = Guid.NewGuid();

                            de.Id = id;

                            double i = Convert.ToDouble(LabelYear.Text);
                        if (i <= 0)
                            {
                                MessageBox.Show("合同年限不能小于等于0哦。");
                            return;
                            }
                            de.ContractLength = i+"年";
                            de.ContractRemarks = txtRemarks.Text;
                            Guid cmbEmployId = (Guid)CmbEmployeeName.SelectedValue;

                            de.EmployeeNameId = cmbEmployId;
                         //   de.EmployeeName = CmbEmployeeName.Text;
                            de.EmployeeSiged = de.ContractSigned.ToString(dateFormat);
                            de.EmployeeExpires = de.ContractExpires.ToString(dateFormat);
                     
                        
                 new ContractInfoDAL().InsertContractInfo(de);
                        //填入合同ID与员工编号ID
        new EmployeeInfoDAL().UpdatEmployeeInfoByContractId(cmbEmployId, de.Id);
                            DialogResult = true;
                        }

#endregion
                    else
                        {
                            if (CmbEmployeeName.SelectedValue !=null)
                                {
                                        de.EmployeeName = CmbEmployeeName.Text;
                              
                    
                           
                            if (SignedDateTime.SelectedDate != null)
                                {
                                    de.ContractSigned = (DateTime)SignedDateTime.SelectedDate;
                                    if (ExpiresDateTime.SelectedDate != null)
                                        {
                                            de.ContractExpires = (DateTime) ExpiresDateTime.SelectedDate;
                                        }

                                }
                                   
                                }
                            else
                                {
                                    MessageBox.Show("不能为空哦。亲");
                                    return;
                                }
                            de.EmployeeSiged = de.ContractSigned.ToString(dateFormat);
                            de.EmployeeExpires = de.ContractExpires.ToString(dateFormat);
                            de.ContractRemarks = txtRemarks.Text;
                            de.ContractLength = LabelYear.Text;
                            de.Id = EditGuid;
                        new ContractInfoDAL().UpdateContractInfo(de);
                            DialogResult = true;

                        }
                }

                private void BtnCancel_Click( object sender, RoutedEventArgs e )
                {
            
                    DialogResult = false;
                    return;

                }

           
            /// <summary>
            /// 当签订合同时间变化时
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
                private void SignedDateTime_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
                {
                if (IsInsert)
                    {
                        if (SignedDateTime.SelectedDate == DateTime.Now.Date)
                        {
                            DateTime oo = DateTime.Now.Date.AddYears(1);
                            ExpiresDateTime.SelectedDate = oo.AddDays(-1);

                        }
                        else
                        {
                          
                                if (SignedDateTime.SelectedDate != null)
                                    {
                                        signTime = (DateTime)SignedDateTime.SelectedDate;
                                    }
                       
                            DateTime oo = signTime.AddYears(1);
                            ExpiresDateTime.Text = oo.ToLongDateString();

                        }
                    }
        
                    }
         
                   
                 


                

                private void ExpiresDateTime_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
                {
                    if (IsInsert)
                        {
                            if (ExpiresDateTime.SelectedDate != null)
                            {
                                if (SignedDateTime.SelectedDate != null)
                                {

                                    TimeSpan span = (DateTime)ExpiresDateTime.SelectedDate - (DateTime)SignedDateTime.SelectedDate;
                                    double i = span.Days;


                                    double t = (i / 365);

                                    LabelYear.Text = t.ToString();
                                }


                            }
                        }
               
                   
                }

            }
    }